//
//  HouseIntroducedCell.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/8.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HouseIntroducedCell.h"
#import "StringTool.h"
#import "config.h"
#import "UIView+circular.h"

#define NameFont [UIFont systemFontOfSize:13]

@interface HouseIntroducedCell()
-(IBAction)clickBtn:(UIButton *)btn;

@property (weak, nonatomic) IBOutlet UIButton *qieHuanBtn;

@property (copy, nonatomic) NSString *detail;
@property (copy, nonatomic) NSString *detailEnglish;

@property (nonatomic,strong)UIView *view;//作为lab的背景
@property (nonatomic,strong)UILabel *detailab;//房屋介绍的内容

@property (weak, nonatomic) IBOutlet UIView *detailView;
@property (nonatomic,strong)NSLayoutConstraint *layConst;//detailVIEW的高度约束
@property (weak, nonatomic) IBOutlet UIView *bttomView;
@property (nonatomic,assign)NSInteger temp;

@end

@implementation HouseIntroducedCell
@synthesize detailab,view;

+(instancetype)cellWithTableView:(UITableView *)tableView WithDetail:(NSString *)detail WithDetailEnglish:(NSString *)detailEnglish
{
      HouseIntroducedCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"HouseIntroducedCell" owner:nil options:nil]lastObject];
    
    for (NSLayoutConstraint *layCons in cell.detailView.constraints) {
        
        if (layCons.firstItem == cell.detailView && layCons.firstAttribute == NSLayoutAttributeHeight) {
            
            cell.layConst = layCons;
            break;
        }
    }
    [cell.qieHuanBtn viewToCircular];
    
    if (![detail isEqualToString:@""]) {
        
        CGFloat labY = 34;
        CGFloat labX = 10;
        CGFloat labW = UIScreenWidth - 2*labX;
        
        
        NSDictionary *attrs =@{NSFontAttributeName:NameFont};
        CGSize nameMaxSize = CGSizeMake(labW,MAXFLOAT);
        
        cell.detailab = [[UILabel alloc]init];
        [cell.detailab setBackgroundColor:[UIColor whiteColor]];
        [cell.detailab setFont:[UIFont systemFontOfSize:13]];
        [cell.detailab setTextColor:UIColorFromRGB(0x999999)];
        cell.detailab.numberOfLines = 0;
        
        
        cell.detailab.text = detail;
        
        CGFloat labH =  [cell.detailab.text boundingRectWithSize:nameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
        
        cell.detailab.frame = CGRectMake(10,  9, UIScreenWidth - 20, labH + 5);
        [cell.detailView addSubview:cell.detailab];

    }
    
    cell.layConst.constant = cell.detailab.frame.size.height + 5*2;
    [cell.detailView layoutIfNeeded];
    if ([StringTool isBlankString:detailEnglish] || [detailEnglish isEqualToString:@""]) {
        
        for (NSLayoutConstraint *layout in cell.bttomView.constraints) {
            if (layout.firstAttribute == NSLayoutAttributeHeight && layout.firstItem == cell.bttomView) {
                
                layout.constant = 0;
                cell.temp = 45;
                break;
            }
        }
        cell.qieHuanBtn.hidden = YES;
    }else if(![StringTool isBlankString:detailEnglish] && ![detailEnglish isEqualToString:@""])
    {
        for (NSLayoutConstraint *layout in cell.bttomView.constraints) {
            if (layout.firstAttribute == NSLayoutAttributeHeight && layout.firstItem == cell.bttomView) {
                
                layout.constant = 45;
                cell.temp = 0;
                break;
            }
        }

        cell.qieHuanBtn.hidden = NO;
    }
    cell.detail = detail;
    cell.detailEnglish = detailEnglish;
     [cell  setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


+(CGFloat)getHeightWithWithDetail:(NSString *)detail WithDetailEnglish:(NSString *)detailEnglish WithTableView:(UITableView *)tableView;
{
    if ([detail isEqualToString:@""]) {
        return 34;
    }
    HouseIntroducedCell *cell = [HouseIntroducedCell cellWithTableView:tableView WithDetail:detail WithDetailEnglish:detailEnglish];
    
//           return cell.view.frame.size.height + cell.view.frame.origin.y;
    return 180 + (cell.layConst.constant - 100)- cell.temp;
  
}
-(IBAction)clickBtn:(UIButton *)btn;
{
    
    if (btn.tag == 0)
    {
        self.detailab.text = self.detailEnglish
        ;
       
        btn.tag = 1;
    }
    else
    {
         self.detailab.text = self.detail;
        btn.tag = 0;
    }
}


@end
